﻿@*
    // Copyright (c) Alex Reich.
    // Licensed under the CC BY 4.0 License.
*@
@using RulesEngineEditor.Models
@inject RulesEngineEditor.Services.WorkflowService WorkflowService

@if (rules != null)
{
    <button class="reeditor_button @IsPressed(sort, "button_depressed")" @onclick="(() => sort = !sort)">Arrange</button>
    <span>&nbsp;</span>
    @if (sort)
    {
        <Dropzone TItem="RuleData" Items="rules" OnItemDrop="@((i)=>RuleDragEnd(i))">
            <div class="rules_arrange">
                <RuleEditor rule="@context" RuleDelete="DeleteRule" />
            </div>
        </Dropzone>
    }
    else
    {
        @foreach (RuleData context in rules)
        {
            <div class="rules">
                <RuleEditor rule="@context" RuleDelete="DeleteRule" />
            </div>
        }
    }
}
@code {
    public bool sort;
    private List<RuleData> _rules;
    [Parameter]
    public List<RuleData> rules
    {
        get { return _rules; }
        set { _rules = value; StateHasChanged(); }
    }

    [Parameter]
    public EventCallback<List<RuleData>> RulesChanged { get; set; }

    public override async Task SetParametersAsync(ParameterView parameters)
    {
        await base.SetParametersAsync(parameters);
    }

    private void DeleteRule(RuleData sp)
    {
        WorkflowService.DeleteRule(rules, sp);
        RulesChanged.InvokeAsync(rules);
    }

    private void RuleDragEnd(RuleData sp)
    {
        WorkflowService.Sort(rules);
        RulesChanged.InvokeAsync(rules);
    }
    public string IsPressed(bool state, string suffix)
    {
        return state ? "reeditor_" + suffix : "";
    }
}
